﻿
using System.Collections.Generic;

using Gola.VN.Scb.Web.Common;
using Gola.VN.Scb.Web.Common.Entities;

namespace Gola.VN.Scb.Web.Dal
{
    public class MainDao
    {
        private static MainDao _mainDao;
        private SqlDao _sqlDao;
        private MemoryDao _memoryDao;

        private MainDao()
        {
        }

        public static MainDao GetInstant()
        {
            if (_mainDao == null)
            {
                _mainDao = new MainDao();
                _mainDao._sqlDao = new SqlDao();
                _mainDao._memoryDao = new MemoryDao();
            }

            return _mainDao;
        }

        public void Execute(DaoPackage package)
        {
            switch (package.Channel)
            {
                case ChannelType.Sql:
                    ExecuteSql(package);
                    break;
                case ChannelType.Memory:
                    ExecuteMemory(package);
                    break;
                default:
                    break;
            }
        }

        private void ExecuteSql(DaoPackage package)
        {
            switch (package.Event)
            {
                case DaoEvent.GetProducts:
                    package.Output = _sqlDao.GetAllProducts();
                    break;
                case DaoEvent.InsertProduct:
                    package.Output = _sqlDao.InsertProduct(package.Input as ProductEntity);
                    break;
                case DaoEvent.UpdateProduct:
                    package.Output = _sqlDao.UpdateProduct(package.Input as ProductEntity);
                    break;
                case DaoEvent.GetCategoriesAsFlat:
                    package.Output = _sqlDao.GetCategoriesAsFlat();
                    break;
                case DaoEvent.GetCategoriesAsTree:
                    package.Output = _sqlDao.GetCategoriesAsTree();
                    break;
                case DaoEvent.GetProviders:
                    package.Output = _sqlDao.GetAllProviders();
                    break;
                case DaoEvent.GetSysInfo:
                    package.Output = _sqlDao.GetSysInfo();
                    break;
                case DaoEvent.UpdateSysInfo:
                    package.Output = _sqlDao.UpdateSysInfo();
                    break;
                case DaoEvent.GetLocations:
                    package.Output = _sqlDao.GetLocations();
                    break;
                case DaoEvent.GetSearchHistories:
                    package.Output = _sqlDao.GetSearchHistories();
                    break;
                case DaoEvent.UpdateSearchHistory:
                    package.Output = _sqlDao.UpdateSearchHistory(package.Input as SearchHistoryEntity);
                    break;
                default:
                    break;
            }
        }

        private void ExecuteMemory(DaoPackage package)
        {
            switch (package.Event)
            {
                case DaoEvent.GetProducts:
                    package.Output = _memoryDao.GetProducts();
                    break;
                case DaoEvent.SetProducts:
                    _memoryDao.SetProducts(package.Input as IEnumerable<ProductEntity>);
                    break;
                case DaoEvent.InsertProduct:
                    _memoryDao.InsertProduct(package.Input as ProductEntity);
                    break;
                case DaoEvent.UpdateProduct:
                    _memoryDao.UpdateProduct(package.Input as ProductEntity);
                    break;
                case DaoEvent.SetCategoriesAsTree:
                    _memoryDao.SetCategoriesAsTree(package.Input as List<CategoryEntity>);
                    break;
                case DaoEvent.GetCategoriesAsTree:
                    package.Output = _memoryDao.GetCategoriesAsTree();
                    break;
                case DaoEvent.SetCategoriesAsFlat:
                    _memoryDao.SetCategoriesAsFlat(package.Input as IEnumerable<CategoryEntity>);
                    break;
                case DaoEvent.GetCategoriesAsFlat:
                    package.Output = _memoryDao.GetCategoriesAsFlat();
                    break;
                case DaoEvent.GetProviders:
                    package.Output = _memoryDao.GetProviders();
                    break;
                case DaoEvent.SetProviders:
                    _memoryDao.SetProviders(package.Input as IEnumerable<ProviderEntity>);
                    break;
                case DaoEvent.SetSysInfo:
                    _memoryDao.SetSysInfo((int)package.Input);
                    break;
                case DaoEvent.GetSysInfo:
                    package.Output = _memoryDao.GetSysInfo();
                    break;
                case DaoEvent.GetLocations:
                    package.Output = _memoryDao.GetLocations();
                    break;
                case DaoEvent.SetLocations:
                    _memoryDao.SetLocations(package.Input as IEnumerable<LocationEntity>);
                    break;
                case DaoEvent.SetSearchHistories:
                    _memoryDao.SetSearchHistories(package.Input as List<SearchHistoryEntity>);
                    break;
                case DaoEvent.GetSearchHistories:
                    package.Output = _memoryDao.GetSearchHistories();
                    break;
                default:
                    break;
            }
        }
    }
}